<?php

class AuthController extends Zend_Controller_Action {

    public function init() {
        $this->view->headTitle('zendblog')->headTitle($this->getRequest()->getActionName())->setSeparator(' :: ');
        $this->view->title = $this->getRequest()->getActionName();
    }

    public function loginAction() {
        $loginForm = new Application_Form_Login();

        if ($this->_hasParam('username')) {
            $loginForm->populate(array('name' => $this->_getParam('username')));
            $this->view->loginForm = $loginForm;
            return;
        }

        if (!$this->getRequest()->isPost()) {
            $this->view->loginForm = $loginForm;
            return;
        } elseif (!$loginForm->isValid($_POST)) {
            $this->view->isInvalid = true;
            $this->view->loginForm = $loginForm;
            return;
        }

        $db = Zend_Db_Table::getDefaultAdapter();
        $result = $db->fetchAll($db->select()
                                ->from(array('u' => 'users'))
                                ->where('u.username=?', $loginForm->getValue('name'))
                                ->where('u.disabled=?', true)
        );

        if (count($result) != 0) {
            $this->view->isDisabled = true;
            return;
        }

        $adapter = new Zend_Auth_Adapter_DbTable(Zend_Db_Table::getDefaultAdapter());
        $adapter->setTableName('users')
                ->setIdentityColumn('username')
                ->setCredentialColumn('password')
                ->setIdentity($loginForm->getValue('name'))
                ->setCredential(hash('SHA256', $loginForm->getValue('password')));

        // authentication 
        $auth = Zend_Auth::getInstance();

        if ($auth->authenticate($adapter)->isValid()) {
            $auth->getStorage()
                    ->write($adapter->getResultRowObject(null, 'password'));
            $this->_helper->redirector('index', 'account', 'admin');
        } else {
            $this->view->isAuthInvalid = true;
            $this->view->loginForm = $loginForm;
        }
    }

    public function logoutAction() {
        Zend_Auth::getInstance()->clearIdentity();
        $this->_helper->redirector('index', 'index');
    }

    public function registerAction() {
        $registerForm = new Application_Form_Register();
        if (!$this->getRequest()->isPost()) {
            $this->view->registerForm = $registerForm;
            return;
        } elseif (!$registerForm->isValid($_POST)) {
            $this->view->isInvalid = true;
            $this->view->registerForm = $registerForm;
            return;
        }

        $tableUsers = new Application_Model_DbTable_Users();
        $data = array(
            'name' => $registerForm->getValue('name'),
            'username' => $registerForm->getValue('username'),
            'password' => hash('SHA256', $registerForm->getValue('password')),
            'email' => $registerForm->getValue('email')
        );
        $tableUsers->insert($data);
        $this->_helper->getHelper('Redirector')->gotoSimple('login', 'auth', null,
                array('username' => $registerForm->getValue('username')));
    }

}
